﻿function addCart(){
     var zk=document.getElementById("Hid_ZK")
     if(zk.value.length<1)
     {
        window.location.href="../Users/Login.aspx";
        return;
     }
     //debugger;
     if(document.getElementById("colors")==null)
     {
        alert("此商品不存在或已经下架!");
        return;
     }
    if(document.getElementById("sizeList").selectedIndex == "0")
    {
        alert("请选择尺寸！");
        return;
    }

    var amount = document.getElementById("Amount").value;
    if (isNaN(amount)) {
        alert("选购商品的数量含有非数字字符");
        document.getElementById("Amount").focus();
        return;
    }
    
    if(amount.indexOf(".")!=-1)
    {
        alert("选购商品的数量必须是整数");
        document.getElementById("Amount").focus();
        return;
    }

    if(document.getElementById("Amount").value == "" || document.getElementById("Amount").value == "0"){
        alert("选购商品的数量不能为空或为零");
        return;
    }
  //现在不检查缺货状态,故注掉  
//    var bWOProduct = false;    
//    // 检查缺货状态，
//    // 这里检查错误信息状态，不是非常可靠，但可以减少一次数据库调用
//    // 由于后面还需要后台验证，这里没有检查出来缺货是可以接受的

//    // 但要是不缺货，却检查成缺货就是个大问题了

//    //
//    var status = document.getElementById("productStatus");
//    if (status != null)
//    {
//      var htm = status.value;
//      if (htm != null && htm != "")
//      {
//        if(htm == "F" || htm == "WO")
//        {
//            var err = "您选购的商品目前缺货!不能加入购物车!";
//            alert(err);
//            return;
//         }
//         else if(htm == "WI")
//         {
//            var err = "您选购的商品目前缺货，在未来一段时间(一个月内)将会到货，建议您先行预定。是否订购？"
//            if(!confirm(err))
//            {
//                return;
//            }
//            else
//            {
//                bWOProduct = true;
//            }
//         }
//      }
//    }
AddSingleProductToCart();

}
function AddSingleProductToCart()
{
   //商品编码
    var Pro = document.getElementById("ProductCode");
    var productID = "";
    if (Pro != null)
        productID = Pro.innerHTML;
    //颜色
    var color=document.getElementById("Hid_SelectColor");
    var colorvalue="";
    if(color!=null)    
    {
        colorvalue=color.value;
    }
    //尺寸
    var sizeList = document.getElementById("sizeList");
    var size = "";
    if (sizeList != null)
        size = sizeList.value;
    //数量        
    var amountInput = document.getElementById("Amount");
    var amount = "";
    if (amountInput != null)
        amount = amountInput.value;
    //标准售价
    var bzsj=document.getElementById("DPJ")
    var bzsjvalue="";
    if(bzsj!=null)
    {
        bzsjvalue=bzsj.innerHTML;
    }
    
    //会员价    
    var member=document.getElementById("MemberPrice");
    var memberprice="";
    if(member!=null)
    {
        memberprice=member.innerHTML;
    }
    //折扣
    var zk=document.getElementById("Hid_ZK");
    var zkvalue="";
    if(zk!=null)
    {
        zkvalue=zk.value;
    }
    //重量
    var O_weight=document.getElementById("Weight");
    var Weight="0";
    if(O_weight!=null)
    {
        Weight=O_weight.innerText;
    }
   $j.post("../Handler/ShoppingCartHandle.ashx", { ProductID: productID,Color:colorvalue,Size:size, Amount:amount,BZSJ:bzsjvalue,MemberPrice:memberprice,ZK:zkvalue,Weight:Weight,OP:"CheckCart"},
        function(data){ 
        if(data=="NoSP")
        {
            if(confirm("此款库存暂缺，但是还会到货，是否继续?"))
            {
                showCart();
                setCartContent("<br /><br />数据加载中......<br /><br /><img src='../images/ajax-loader.gif'>"); 
                $j.post("../Handler/ShoppingCartHandle.ashx", { ProductID: productID,Color:colorvalue,Size:size, Amount:amount,BZSJ:bzsjvalue,MemberPrice:memberprice,ZK:zkvalue,Weight:Weight,OP:"add"},
                        function(data){ 
                                        setCartContent(data);
                                       }
                        );}
            else
            return;
        }
        else 
        if(data=="true")
        {
                showCart();
                setCartContent("<br /><br />数据加载中......<br /><br /><img src='../images/ajax-loader.gif'>"); 
                $j.post("../Handler/ShoppingCartHandle.ashx", { ProductID: productID,Color:colorvalue,Size:size, Amount:amount,BZSJ:bzsjvalue,MemberPrice:memberprice,ZK:zkvalue,Weight:Weight,OP:"add"},
                            function(data){ 
                                setCartContent(data);
                                            }
                        );}
        else 
        if(data.slice(-5).toString()=="false")
        {
              alert(data.split("|")[0]);
                return;
        }
        else 
        if(data=="NoReStock")
        {
            alert("没有可补货信息,不能购买");
            return;
        }
        else
        {
            if(confirm(data))
            {
                showCart();
                setCartContent("<br /><br />数据加载中......<br /><br /><img src='../images/ajax-loader.gif'>"); 
                $j.post("../Handler/ShoppingCartHandle.ashx", { ProductID: productID,Color:colorvalue,Size:size, Amount:amount,BZSJ:bzsjvalue,MemberPrice:memberprice,ZK:zkvalue,Weight:Weight,OP:"add"},
                        function(data){ 
                            setCartContent(data);
                                        }
                                );}
            else 
            return;
            } 
        }
    );  
}
function showCart()
{ 
    var div_back = document.getElementById("div_background");
    div_back.style.display = "block"; 
    var iframe_back = document.getElementById("iframe_back");
    iframe_back.style.display = "block";  
    var sizeList = document.getElementById("sizeList"); 
    sizeList.disabled = true; 
    var cart = document.getElementById("ShoppingCart");
    cart.style.display = "block";
    cart.style.position = "absolute";
    var bigImg = document.getElementById("pbigImg");
    var pleft = 0;
    var ptop = 0;
   
    var obj = bigImg.offsetParent;
 
    while(obj.tagName != "BODY")
    {
         pleft = pleft + obj.offsetLeft;
         ptop = ptop + obj.offsetTop;
         obj = obj.offsetParent;
    }
    ptop = ptop + 150;
    cart.style.left = pleft +100+ "px";
    cart.style.top = ptop +"px";
    cart.style.width = 300 + "px";
    cart.style.backgroundColor  = "#ffffff";
}
function hideCart()
{
  var div_back = document.getElementById("div_background");
    div_back.style.display = "none"; 
     var iframe_back = document.getElementById("iframe_back");
    iframe_back.style.display = "none";   
    var cart = document.getElementById("ShoppingCart");
    cart.style.display = "none";
    var sizeList = document.getElementById("sizeList"); 
    sizeList.disabled = false;  
}
function setCartContent(data)
{
    var cart = document.getElementById("ShoppingCart");
    cart.innerHTML = data;
}
function AddCollect()
{
     //商品编码
    var Pro = document.getElementById("ProductCode");
    var productID = "";
    if (Pro != null)
        productID = Pro.innerHTML;
    showCart();
    setCartContent("<br /><br />数据加载中......<br /><br /><img src='../images/ajax-loader.gif'>");   
    $j.post("../Handler/ShoppingCartHandle.ashx", { ProductID: productID,OP:"Collect"},
        function(data){         
            if(data=="true")
            {
                alert("收藏成功");
                if(window.confirm("收藏成功,要查看收藏夹吗?"))
                {
                    window.location.href="../Users/CustomerCollect.aspx";
                }
                hideCart();
            }
            else
            {
                alert("此商品已经收藏!");
                hideCart();
            }
        }
    );
}
//----------------------------------------------------------------ordercart----------------------------------
function InitOrderCart()
{
    SetOrderCart("<br /><br />数据加载中......<br /><br /><img src='../images/ajax-loader.gif'>");   
    $j.post("../Handler/ShoppingCartHandle.ashx", { OP:"Select"},
        function(data){ 
            SetOrderCart(data);
        }
    );
}
function SetOrderCart(data)
{    
    document.getElementById("CartInfo").innerHTML=data;
}
function ModifyCart()
{
    var havep=false;
    var slinput=document.getElementsByTagName("input");
    if(slinput!=null)
    {
        for(var j=0;j<slinput.length;j++)
        {
            var sp=slinput[j];
            if(sp.id!=null)
            {
                if(sp.id.substr(0,5)=="input")
                {
                   havep=true;   
                   break;            
                }
            }        
        }
    }
    if(!havep)
    {
        alert("请先购物!");
        return;
    }
    var modify=document.getElementById("Modify");
    var jiesuan=document.getElementById("JieSuan");
    var GoOn=document.getElementById("GoOn");
    var Submit=document.getElementById("Submit");
    var cancel=document.getElementById("Cancel");
    modify.style.display="none";
    jiesuan.style.display="none";
    GoOn.style.display="none";
    Submit.style.display="block";
    cancel.style.display="block";
    var slspan=document.getElementsByTagName("span");
    var slinput=document.getElementsByTagName("input");
    for(var i=0;i<slspan.length;i++)
    {
        var sp=slspan[i];
        if(sp.id!=null)
        {
            if(sp.id.substr(0,4)=="span")
            {
                sp.style.display="none";
            }
        }
    }
    for(var j=0;j<slinput.length;j++)
    {
        var sp=slinput[j];
        if(sp.id!=null)
        {
            if(sp.id.substr(0,5)=="input")
            {
                sp.style.display="block";
            }
        }
    }
}
function CancelCart()
{
    var modify=document.getElementById("Modify");
    var jiesuan=document.getElementById("JieSuan");
    var GoOn=document.getElementById("GoOn");
    var Submit=document.getElementById("Submit");
    var cancel=document.getElementById("Cancel");
    modify.style.display="block";
    jiesuan.style.display="block";
    GoOn.style.display="block";
    Submit.style.display="none";
    cancel.style.display="none";
    var slspan=document.getElementsByTagName("span");
    var slinput=document.getElementsByTagName("input");
    for(var i=0;i<slspan.length;i++)
    {
        var sp=slspan[i];
        if(sp.id!=null)
        {
            if(sp.id.substr(0,4)=="span")
            {
                sp.style.display="block";
            }
        }
    }
    for(var j=0;j<slinput.length;j++)
    {
        var sp=slinput[j];
        if(sp.id!=null)
        {
            if(sp.id.substr(0,5)=="input")
            {
                sp.style.display="none";
            }
        }
    }
}
function SubmitCart()
{
    var uplist='';
    var slinput=document.getElementsByTagName("input");
    for(var j=0;j<slinput.length;j++)
    {
        var sp=slinput[j];
        if(sp.id!=null)
        {
            if(sp.id.substr(0,5)=="input")
            {
                var pattern=/^[0-9]*[1-9][0-9]*$/;
                var flag=pattern.test(sp.value);
                if(!flag)
                {
                    alert("购物数量只能为正整数!");
                    sp.focus();
                    return;
                }
                else
                {
                    uplist=uplist+sp.id.substr(5,sp.id.length-5)+","+sp.value+"|";               
                }
            }
        }
    }
    MastCart();
    setCartContent("<br /><br />数据加载中......<br /><br /><img src='../images/ajax-loader.gif'>");  
    $j.post("../Handler/ShoppingCartHandle.ashx", {UpList:uplist, OP:"Update"},
        function(data){           
            if(data=="true")
            {
            window.location.reload();
            }
            else
            {
                HidCart();
                alert(data);
            }
        }
    );
}
function MastCart()
{
    var div_back = document.getElementById("div_background");
    div_back.style.display = "block"; 
    var iframe_back = document.getElementById("iframe_back");
    iframe_back.style.display = "block";     
    var cart = document.getElementById("ShoppingCart");
    cart.style.display = "block";
    cart.style.position = "absolute";
    var bigImg = document.getElementById("CartInfo");
    var pleft = 0;
    var ptop = 0;
   
    var obj = bigImg.offsetParent;
 
    while(obj.tagName != "BODY")
    {
         pleft = pleft + obj.offsetLeft;
         ptop = ptop + obj.offsetTop;
         obj = obj.offsetParent;
    }
    ptop = ptop +50;
    cart.style.left = pleft +200+ "px";
    cart.style.top = ptop +"px";
    cart.style.width = 300 + "px";
    cart.style.backgroundColor  = "#ffffff";
}
function HidCart()
{
    var div_back = document.getElementById("div_background");
    div_back.style.display = "none"; 
    var iframe_back = document.getElementById("iframe_back");
    iframe_back.style.display = "none";   
    var cart = document.getElementById("ShoppingCart");
    cart.style.display = "none";    
}
function DelCart(ID)
{
    if(confirm('确定要删除吗!'))
    {
        MastCart();
        setCartContent("<br /><br />数据加载中......<br /><br /><img src='../images/ajax-loader.gif'>");  
        $j.post("../Handler/ShoppingCartHandle.ashx", {ID:ID, OP:"Delete"},
            function(data){           
                if(data=="true")
                {
                window.location.reload();
                }
                else
                {
                    HidCart();
                    alert(data);
                }
            }
        );
    }    
}
function JieSuan()
{
    var havep=false;
    var slinput=document.getElementsByTagName("input");
    if(slinput!=null)
    {
        for(var j=0;j<slinput.length;j++)
        {
            var sp=slinput[j];
            if(sp.id!=null)
            {
                if(sp.id.substr(0,5)=="input")
                {
                   havep=true;   
                   break;            
                }
            }        
        }
    }
    if(!havep)
    {
        alert("请先购物!");
    }
    else
    {
         $j.post("../Handler/ShoppingCartHandle.ashx", {OP:"Check"},
            function(data){           
                if(data.length>1)
                {
                    alert(data);                    
                }
                else
                {                   
                    window.location.href="OrderAddress.aspx";
                }
            }
        );
        
    }
}